<template>
  <el-drawer :title="dialogTilte" :before-close="handleClose" :visible.sync="showDialog" direction="rtl"
    custom-class="drawer-industry-new-modify" ref="drawer" :wrapperClosable="false">
    <div class="drawer-body">
      <el-form ref="refForm" :model="formData" label-position="right" label-width="150px">
        <el-form-item prop="mid_table_remark_001" label="话题标题" :rules="[{ required: true, message: '请输入话题标题' }]">
          <el-input v-model="formData.mid_table_remark_001" autocomplete="off" placeholder="请输入话题标题"></el-input>
        </el-form-item>
        <el-form-item prop="mid_table_remark_002" label="话题描述">
          <el-input v-model="formData.mid_table_remark_002" autocomplete="off" placeholder="请输入话题描述"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" :loading="loading" @click="handleSubmit">{{
            loading ? "提交中 ..." : "确定"
          }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>
<script>
import { parseTime } from "@/utils/index";
import { middleList } from "@/api/middle";
export default {
  name: "TopicNewModify",
  data() {
    return {
      showDialog: false,
      formData: {
        // 表ID
        pre_db_para_id_public_middle_table: "",
        // 话题名称
        mid_table_remark_001: "",
        // 话题描述
        mid_table_remark_002: "",
      },
      loading: false,

    };
  },
  props: {},
  computed:{
    dialogTilte(){
      if(this.formData.pre_db_para_id_public_middle_table||this.formData.pre_db_para_id_public_middle_table===0){
        return "编辑话题"
      }
      return "新增话题"
    }
  },
  methods: {
    onEditOrAdd(row) {
      if (!row) {
        this.formData = {
          // 表ID
          pre_db_para_id_public_middle_table: "",
          mid_table_remark_001: "",
          mid_table_remark_002: "",
        };
      } else {
        // 编辑
        // this.formData = {
        //   // 表ID
        //   pre_db_para_id_public_middle_table: row.id_public_middle_table,
        //   // 行业名称
        //   mid_table_remark_001: row.mid_table_remark_001,
        //   // 行业简介
        //   mid_table_remark_002: row.mid_table_remark_002,
        //   // Embeddings规则Id
        //   // Fine-tuning规则Id
        //   // 执行后调用URL
        //   // mid_table_remark_021: row.mid_table_remark_021,
        // };
      }
      this.showDialog = true;
      this.$nextTick(()=>{
        this.$refs.refForm.clearValidate()
      });
    },
    handleClose() {
      this.showDialog = false;
      if (this.loading) {
        this.$message.error("数据提交中,暂不能关闭窗口");
      }
    },
    handleSubmit() {
      this.$refs.refForm.validate((valid) => {
        if (valid) {
          this.onSubmit();
        }
      });
    },
    onSubmit() {
      if (this.loading) {
        return;
      }
      this.loading = true;
      let postData = {
        invoke_method: "public_insert_or_update_middle_table",
        pre_db_para_middle_table_name: "public_middle_table_lite_mod_forum_bbs",
        type_middle_app: 61001,
        type_inner_app: 1001,
        is_online: 0,
        mid_table_remark_001: this.formData.mid_table_remark_001,
        mid_table_remark_002: this.formData.mid_table_remark_002,
        // pre_db_para_id_public_middle_table: this.formData.id
      };
      if (
        this.formData.pre_db_para_id_public_middle_table ||
        this.formData.pre_db_para_id_public_middle_table === 0
      ) {
        postData.pre_db_para_id_public_middle_table =
          this.formData.pre_db_para_id_public_middle_table;
      } else {
        let dtNow = new Date();
        Object.assign(postData, {
          mid_table_remark_040: parseTime(dtNow, "{y}{m}{d}"),
          mid_table_remark_041: parseTime(dtNow, "{y}{m}"),
          mid_table_remark_042: parseTime(dtNow, "{y}"),
          mid_table_remark_043: dtNow.getTime()
        });
      }
      middleList(this.Qs.stringify(postData)).then(
        (res) => {
          this.loading = false
          this.$notify({
            title: "Success",
            message: "保存成功",
            type: "success",
            duration: 2000,
          });
          this.$emit("refresh");
          this.showDialog = false;

        },
        (error) => {
          this.loading = false
        }
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.drawer-industry-new-modify {
  width: 700px;
  padding-right: 10rpx;
  box-sizing: border-box;

  .drawer-body {
    position: relative;
    width: 100%;
    padding-left: 30px;
    padding-right: 30px;
    box-sizing: border-box;
  }
}
</style>
